package com.example.admin.goodnews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.LiShiDataEntity;
import com.squareup.picasso.Picasso;

/**
 * 历史上的今天适配器
 * Created by admin on 2016/10/15.
 */

public class LiShiAdapter extends RecyclerView.Adapter<LiShiAdapter.ViewHolder> {
    private Context context;
    private LiShiDataEntity liShiDataEntity;// 数据实体类

    public LiShiAdapter(Context context, LiShiDataEntity liShiDataEntity) {
        this.context = context;
        this.liShiDataEntity = liShiDataEntity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_lishi, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String year = liShiDataEntity.getShowapi_res_body().getList().get(position).getYear();
        String month = "" + liShiDataEntity.getShowapi_res_body().getList().get(position).getMonth();
        String day = "" + liShiDataEntity.getShowapi_res_body().getList().get(position).getDay();
        holder.tv_lishi_title.setText(liShiDataEntity.getShowapi_res_body().getList().get(position).getTitle());
        holder.tv_lishi_time.setText(year + "-" + month + "-" + day);

        // 有图
        if (liShiDataEntity.getShowapi_res_body().getList().get(position).getImg() != null) {
            Picasso.with(context).load(liShiDataEntity.getShowapi_res_body().getList().get(position).getImg()).into(holder.iv_lishi_content);
        } else {
            holder.iv_lishi_content.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return liShiDataEntity.getShowapi_res_body().getList().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_lishi_title;// 标题
        private ImageView iv_lishi_content;// 图片
        private TextView tv_lishi_time;// 时间

        public ViewHolder(View itemView) {
            super(itemView);
            tv_lishi_title = (TextView) itemView.findViewById(R.id.tv_lishi_title);// 标题
            iv_lishi_content = (ImageView) itemView.findViewById(R.id.iv_lishi_content);// 图片
            tv_lishi_time = (TextView) itemView.findViewById(R.id.tv_lishi_time);// 时间
        }
    }
}
